package com.tanhua.app.controller;

import com.tanhua.app.Service.CommentsService;
import com.tanhua.app.Service.MovementsService;
import com.tanhua.model.mongo.Movement;
import com.tanhua.model.vo.MovementsVo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.VisitorsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/movements")
public class MovementsController {

    @Autowired
    private MovementsService movementsService;

    @Autowired
    private CommentsService commentsService;

//    动态-发布
//    POST    /movements
    @PostMapping
    public ResponseEntity movements(Movement movement, MultipartFile imageContent[]) throws Exception {
        movementsService.publishMovement(movement,imageContent);
        return ResponseEntity.ok(null);
    }

//    我的动态
//    GET   /movements/all
    @GetMapping("/all")
    public ResponseEntity myMovement(Long userId
            ,@RequestParam(defaultValue = "1") Integer page
            ,@RequestParam(defaultValue = "10") Integer pagesize){
        PageResult<MovementsVo> pr = movementsService.findByUserId(userId,page,pagesize);
        return ResponseEntity.ok(pr);
    }
    //    好友动态
//    GET   /movements
    @GetMapping
    public ResponseEntity movements(@RequestParam(defaultValue = "1") Integer page,
                                    @RequestParam(defaultValue = "10") Integer pagesize){
        PageResult<MovementsVo> pr = movementsService.findFriendMovements(page,pagesize);
        return ResponseEntity.ok(pr);
    }

//    推荐动态
//    GET   /movements/recommend
    @GetMapping("/recommend")
    public ResponseEntity removement(@RequestParam(defaultValue = "1") Integer page,
                                     @RequestParam(defaultValue = "10") Integer pagesize){
        PageResult<MovementsVo> pr = movementsService.findRecommendMovements(page,pagesize);
        return ResponseEntity.ok(pr);
    }

//    单条动态
//    GET   /movements/:id
    @GetMapping("/{id}")
    public ResponseEntity findById(@PathVariable("id") String id){
        MovementsVo vo = movementsService.findById(id);
        return ResponseEntity.ok(vo);
    }

    //    动态点赞
//    GET   /movements/:id/like
    @GetMapping("/{id}/like")
    public ResponseEntity like(@PathVariable("id") String movementId){
        Integer likeCount = commentsService.likeMovement(movementId);
        return ResponseEntity.ok(likeCount);
    }

//    动态-取消点赞
//    GET /movements/:id/dislike
    @GetMapping("/{id}/dislike")
    public ResponseEntity dislike(@PathVariable("id") String movementId){
        Integer likeCount = commentsService.dislikeMovement(movementId);
        return ResponseEntity.ok(likeCount);
    }

//    动态-喜欢
//    GET   /movements/:id/love
    @GetMapping("/{id}/love")
    public ResponseEntity love(@PathVariable("id") String movementId){
        Integer loveCount = commentsService.loveMovement(movementId);
        return ResponseEntity.ok(loveCount);
    }

//    动态-取消喜欢
//    GET   /movements/:id/unlove
    @GetMapping("/{id}/unlove")
    public ResponseEntity unlove(@PathVariable("id") String movementId){
        Integer loveCount = commentsService.unlove(movementId);
        return ResponseEntity.ok(loveCount);
    }

//    谁看过我
//    GET   /movements/visitors
    @GetMapping("/visitors")
    public ResponseEntity queryVisitorsList(){
        List<VisitorsVo> vos = movementsService.queryVisitorsList();
        return ResponseEntity.ok(vos);
    }
}
